package com.s63.appinfo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.s63.appinfo.pojo.AjaxResult;
import com.s63.appinfo.pojo.AppInfo;
import com.s63.appinfo.pojo.LoginUser;
import com.s63.appinfo.service.AppInfoService;
import com.s63.appinfo.util.SessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.s63.appinfo.util.FileUtils.saveFile;

@RestController
@RequestMapping("/appInfo")
public class AppInfoController extends BaseController {
    @Autowired
    private AppInfoService appInfoService;

    /**
     * @param apkName
     * @return 如果存在，返回true
     */
    @GetMapping("/isExist")
    public AjaxResult getByApkName(String apkName) {
        AppInfo appInfo = appInfoService.getByApkName(apkName);
        return appInfo == null ? AjaxResult.ok(false) : AjaxResult.ok(true);
    }

    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        AppInfo appInfo = appInfoService.getById(id);
        return AjaxResult.ok(appInfo);
    }

    @GetMapping("/list")
    public AjaxResult list(AppInfo appInfo, Integer pageNo, Integer pageSize) {
        LoginUser loginUser = SessionManager.getInstance().getCurrentUser();
        PageHelper.startPage(pageNo, pageSize);
        // 限定开发者只显示自己的数据
        if (loginUser.getType().equals("1")) {
            appInfo.setDevId(loginUser.getId());
        }
        // 管理员只显示待审核的数据
        if (loginUser.getType().equals("2")) {
            appInfo.setStatus(1L);
        }

        List<AppInfo> list = appInfoService.get(appInfo);
        PageInfo<AppInfo> pageInfo = PageInfo.of(list);
        return AjaxResult.ok(pageInfo);
    }

    @PostMapping
    public AjaxResult add(AppInfo appInfo, MultipartFile file) throws IOException {
        appInfo.setLogoPicPath(saveFile(file));
        appInfo.setDevId(SessionManager.getInstance().getCurrentUser().getId());
        appInfo.setCreationDate(new Date());
        appInfo.setStatus(1L);
        appInfoService.add(appInfo);
        return AjaxResult.ok();
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id) {
        boolean success = appInfoService.del(id);
        return AjaxResult.ok(success);
    }

    @PostMapping("/update")
    public AjaxResult update(AppInfo appInfo, MultipartFile file) throws IOException {
        appInfo.setStatus(1L);
        appInfo.setLogoPicPath(saveFile(file));
        appInfoService.update(appInfo);
        return AjaxResult.ok();
    }

    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody AppInfo appInfo) throws IOException {
        appInfo.setUpdateDate(new Date());
        appInfo.setModifyBy(SessionManager.getInstance().getCurrentUser().getId());
        appInfoService.update(appInfo);
        return AjaxResult.ok();
    }
}
